﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace XNAGameFeatures.BasicGame.Screens
{
    /// <summary>
    /// Base class for a load screen. It will be use during the call 
    /// of a <see cref="Screen.LoadContent"/> method of a <see cref="GameScreen"/>
    /// </summary>
    public abstract class LoadScreen : Screen
    {
        private Thread _loading;
        private GameScreen _gameScreen;

        protected LoadScreen(GameScreen gameScreen)
        {
            _gameScreen = gameScreen;
        }

        private void ThreadLoading()
        {
            _gameScreen.LoadContent();
        }

        public override void LoadContent()
        {
            _gameScreen.ScreenManager = ScreenManager;
            _loading = new Thread(ThreadLoading);
            _loading.Start();
            base.LoadContent();
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Update(gameTime);

            if (_loading.ThreadState == ThreadState.Stopped)
            {
                _gameScreen.IsContentLoaded = true;
                ScreenManager.RemoveScreen(this);
                ScreenManager.AddScreen(_gameScreen);
            }
        }
    }
}
